import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:macnov/stores/wallet.dart';
import 'package:macnov/utils/colorUtil.dart';

class AddCard extends StatefulWidget {
  @override
  _AddCardState createState() => _AddCardState();
}

class _AddCardState extends State<AddCard> {
  GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

  String _nickName;

  void _forSubmitted(BuildContext context) {
    var _form = _formKey.currentState;
    if (_form.validate()) {
      _form.save();
      _saveCard(context);
    }
  }

  void _saveComplete(BuildContext context) {
    Navigator.pop(context);
    walletStore.loadVCards();
  }

  _saveCard(BuildContext context) {
    walletStore.addVCard(_nickName, () => _saveComplete(context));
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
      statusBarColor: Colors.white,
    ));
    return new Scaffold(
        appBar: new AppBar(
          backgroundColor: Colors.white,
          brightness: Brightness.light,
          iconTheme: IconThemeData(color: Colors.black),
          elevation: 0.0,
          title: new Text('NEW CARD',style: TextStyle(color: Colors.black),),
          actions: <Widget>[
            FlatButton(
              child: Text(
                'SAVE',
                style: TextStyle(color: Colors.black),
              ),
              onPressed: () => _forSubmitted(context),
            )
          ],
        ),
        body: SingleChildScrollView(
          padding: EdgeInsets.all(10),
          child: Column(
            children: <Widget>[
              new Form(
                key: _formKey,
                child: new Column(
                  children: <Widget>[
                    new TextFormField(
                      style:
                          TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
                      decoration: new InputDecoration(
                        labelText: 'CARD NAME',
                        labelStyle: TextStyle(
                            fontSize: 16, color: ColorsUtil.hexColor(0x999999)),
                      ),
                      validator: (val) {
                        return val.length < 4
                            ? "card name less than 4 letter"
                            : null;
                      },
                      onSaved: (val) {
                        _nickName = val;
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ));
  }
}
